package com.bacon.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2021-01-10 10:55
 * @description:
 */
@Configuration
public class DeadLetterMqConfig {
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    //普通队列
    @Bean
    public Queue queueDeadLetter2() {
        return QueueBuilder.durable(queue_dead_2).build();
    }

    //TTL 队列
    @Bean
    public Queue queueDeadLetter1(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", exchange_dead);
        map.put("x-dead-letter-routing-key", routing_dead_2);
        //全局设置 过期时间。 优先级低于局部设置
        map.put("x-message-ttl", 10 * 1000);
        return  QueueBuilder.durable(queue_dead_1).withArguments(map).build();
    }

    @Bean
    public Exchange exchangeDeadLetter(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }

    //路由绑定
    @Bean
    public Binding bindingDeadLetter1(){
        return BindingBuilder.bind(queueDeadLetter1()).
                to(exchangeDeadLetter()).with(routing_dead_1).noargs();
    }

    @Bean
    public Binding bindingDeadLetter2(){
        return BindingBuilder.bind(queueDeadLetter2()).
                to(exchangeDeadLetter()).with(routing_dead_2).noargs();
    }

}
